package nku.csc364;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/* PriorityQueue.java
 * Raj Kiran Akula
 * CSC 364	
 * 
 * Client code to run the generic class to implement a hospital ER queue.
 *
 */
 
public class Triage {
	
	String backUpFileName = "backup.txt"; //File for backing up all the data
	int queueSize;  // Size of Queue to be specified
	PriorityQueue hospitalQueue; //Hospital Queue
	
	public Triage()throws IOException{
		
		//Opening up backUpfile
		File backUpFile = new File(backUpFileName);
		
		//If there is such a file then it instantiates the queue according
		// to the number of priorities specified in the file
		// Also add's all saved patients in the file
		
		if (backUpFile.exists()){
			
			String line; // to read line
			String[] lineArray = null; // to break up each line into fields
			FileReader input = new FileReader(backUpFileName); //Opens file reader
			BufferedReader bufRead = new BufferedReader(input); // Opens buffered reader
			line = bufRead.readLine(); // Read's line
			queueSize = Integer.parseInt(line); //Converts queue Size from string to integer
			hospitalQueue = new PriorityQueue(queueSize); //Instantiates priority queue size
			line = bufRead.readLine(); // Read line for patients info
			
			//Keeps reading patient details
			while (line != null){
                lineArray = line.split(" ");//Splits line words on space
           		hospitalQueue.enqueue(lineArray[2], Integer.parseInt(lineArray[4]), lineArray[6]); //Reinstanties queue
                line = bufRead.readLine();//Read next line
             }
             
			System.out.println("There was a back up file, so priority queue has been updated!!!!");
			//Runs program now!!!
			displayMenu();
		}
		else{
			//Checks size of queue from user first
			checkQueueSize();
			hospitalQueue = new PriorityQueue(queueSize); //Instantiates queue
			//Runs program now!!!
			displayMenu();
		}
	}
	
	//Program main.. i made it like this so that i can easily change the project to a GUI Project
	public static void main(String... args)throws IOException{
		new Triage();
	}
	
	//Method to read the number of queues
	public void checkQueueSize()throws IOException{
		String size; // temp variable for storing size as a string
		System.out.print("Please enter the number of priorities: "); // User prompt
		
		//To read from user
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		//Reading from user
		size = in.readLine();
		
		//Converting String number to Integer number
		try{
			queueSize = Integer.parseInt(size);
		}
		catch(NumberFormatException nfe){
			System.out.println("Please reenter the size of queue, if you don't enter a proper number this program will crash!!!");
			System.out.print("Please enter the number of priorities: "); // User prompt
			size = in.readLine();
			queueSize = Integer.parseInt(size);
			
		}
	}
	
	
	public void displayMenu()throws IOException{
		
		
		String choice; // String to store choice number temporarily
		int choiceInt = 0; // String formatted into integer
		
		//Displays menu till a correct choice is entereed
		while(choiceInt != 4){
	
			//Menu Bar
			System.out.println("Menu");
			System.out.println("1) Enqueue");	
			System.out.println("2) Dequeue");
			System.out.println("3) Priority 1 Dump");
			System.out.println("4) Quit");
			System.out.print("Please select one of the options above: ");
			//Gets input from user
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			choice = in.readLine();
			
			//Converts to number
			try{
				choiceInt = Integer.parseInt(choice);
			}
			catch(NumberFormatException nfe){
				System.out.println("Enter a number choice, you enter a non-numeric input again, this program will crash!!!");
				choice = in.readLine();
				choiceInt = Integer.parseInt(choice);
				
			}
			//If incorrect choice is entered a prompt is displayed
			if((choiceInt < 1) || (choiceInt > 4)){
				System.out.println("You entered an incorrect choice please enter again!!!");
			}
			
			else{
				
				//If enqueue is chose this happens
				if (choiceInt == 1){
					
					//Temporary variables used for storage
					String name;
					int priority = 0;
					String dateString;
					String tempPriority;
					
					//User prompt
					System.out.print("Please enter the patient's name: ");
					name = in.readLine(); //Read User data
					
					while(priority < 1 || priority > queueSize){
						//User Prompt	
						System.out.print("Please enter the patient's priority: ");
						tempPriority = in.readLine(); //Read User data
						try{
							priority = Integer.parseInt(tempPriority);// Convert to integer
							if(priority < 1 || priority > queueSize){
								System.out.println("You entered an incorrect priority!!!");
							}
						}
						catch(NumberFormatException nfe){
							System.out.println("You entered an non-numeric key, Reenter correctly or program will crash!!!");
							System.out.print("Please enter the patient's priority: ");
							tempPriority = in.readLine(); //Read User data
							priority = Integer.parseInt(tempPriority);// Convert to integer
							if(priority < 1 || priority > queueSize){
								System.out.println("You entered an incorrect priority!!!");
							}
						}
					}
					//Get date
					SimpleDateFormat sdf = new SimpleDateFormat( "hh:mm:ss" );
  					Calendar date = Calendar.getInstance();
    				dateString =  sdf.format(date.getTime());
    				
    				//Insert into queue
					hospitalQueue.enqueue(name, priority, dateString);
					
				}
				//To dequeue
				else if (choiceInt == 2){
					hospitalQueue.dequeue();
				}
				//To display priority one
				else if (choiceInt == 3){
					hospitalQueue.priorityOneDump();
				}
				//To exit
				else if (choiceInt == 4){
					System.out.println("Good Bye!!!");
				}
			}
		}
	}
	
	
}